home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.mactech.com 2010
/
ftp.mactech.com.tar
/
ftp.mactech.com
/
machack
/
Hacks97
/
PickMeUp.sit
/
Pick Me Up
/
source code
/
Movie app source
/
pickmeUp97
/
source
/
scaleDrawRect.cp
< prev
next >
Wrap
Text File
|
1997-06-27
|
4KB
|
116 lines
//===========================================================================
// •• ScaleDrawRect
//===========================================================================
void
LPictThumbView::ScaleDrawRect( Rect inOriginalRect, Rect inThumnailRect,
Rect* outScaledRect)
{
/*
NOTES:
This routine is for scaling the drawRect of a picture to fit into
a thumbnail sized drawing area. For example, if you have a picture
that is 500 x 700 pixels, but only have a 70 x 70 pixel size
area in which to draw the image, this routine will scale
the picture down.
This routine will also scale a rect(outScaledRect) so that it will center
itself in the thumnail. This way, the picture that is scaled down will
be centered when it is drawn in the thumbnail.
Why only Rects?
Generally when drawing pictures, the system routines that do the actual
drawing want a Rect to define the size and location of the picture in the
current GrafPort. that is why all that is needed here is to scale the
rect and then pass outScaledRect to the draw routine.
Rect inOriginalRect; Input the original size of the picture's rect here.
Rect inThumbnailRect; Input the size of the thumnail where the picture is
supposed to be drawn here.
Rect* outScaledRect; This is where the scaled rect for the picture
will be passed back to you.
*/
short percent = 0;
::InsetRect(&inThumnailRect, 1,1);
// Getting the h and w of the orig pics frame size.
short w = inOriginalRect.right - inOriginalRect.left;
short h = inOriginalRect.bottom - inOriginalRect.top;
// Getting the h and w of the thumbs frame size.
short Aw = inThumnailRect.right - inThumnailRect.left;
short Ah = inThumnailRect.bottom - inThumnailRect.top;
// Comparing which is bigger with the orig pics
// Why do we have to compare? In order to scale the picture, we
// need to know which side is bigger. That way, depending on
// which one is bigger, the bigger side will be the full size
// of the thumbnail window, and then we'll scale the other
// side down to match the other side.
if(w > h)
{
// Since the width is larger than the height, we are here.
// We first set the left of inOriginalRect to inThumbnailRects left,
// plus some offset. inThumnailRect was inset by 1, and the offset
// is just found by experimenting.
inOriginalRect.left = inThumnailRect.left + 1;
// Since the width is bigger than the height, we set the
// width to be the size of inThumnailRects width. That way the
// entire width will fit into the thumbnail rect.
inOriginalRect.right = Aw;
// Now we need to know what percentage the image was scaled down.
// We need to know this because we need to scale the other side
// just as much as we scaled the the width.
percent = ((Aw * 100)/w);
// Now we take the percent and determine a pixel amount based on the
// percentage we scaled.
inOriginalRect.bottom = ((percent * h)/100);
// Now we need to center the newly scaled rect in the thumbnail
// rect. If we didn't do this, then the upper left corner of the
// image would be at the upper left corner of the thumbnail. We want
// the image to be in the middle of the thumbnail so we kluge here.
inOriginalRect.top = ((Ah - inOriginalRect.bottom)/2);
// Just some fudging to get the centering just right. This is found
// with experimentation.
inOriginalRect.top += 1;
// Since we just collapsed the rect, top down, we need to expand the
// bottom of the rect as much as we collapsed it from top.
inOriginalRect.bottom += inOriginalRect.top;
}else if(w < h)
{
inOriginalRect.top = inThumnailRect.top + 1;
inOriginalRect.bottom = Ah;
percent = ((Ah * 100)/h);
inOriginalRect.right = ((percent * w)/100);
inOriginalRect.left = ((Aw - inOriginalRect.right)/2);
inOriginalRect.left += 2;
inOriginalRect.right += inOriginalRect.left;
}else if(w == h)
{
InsetRect(&inThumnailRect, 1,1);
inOriginalRect = inThumnailRect;
}
// Now passing the scaled rect back into outScaledRect
::BlockMoveData(&inOriginalRect, outScaledRect, sizeof(Rect));
return;
}